package com.wry.wry_test.controller;

import com.wry.wry_test.feign.api.ApiServiceFeign;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.Unsafe;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: wangrongyi
 * @Date: 2024/7/12 17:17
 * @Description:
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private ApiServiceFeign apiServiceFeign;

    private final ReentrantLock lock = new ReentrantLock();

    @GetMapping("/testFeign")
    void testFeign() throws IOException {
        Response response = apiServiceFeign.getAllUrl("services", "GET");
        if (response == null) return;

        InputStream in = response.body().asInputStream();
        String result = new String(in.readAllBytes());
//        log.info("response = {}", result);

    }


    @GetMapping("/testProxy")
    void test() {
        apiServiceFeign.testProxy();
        log.info("testProxy");
    }


//    @Transactional
//    @GetMapping("/testProxy")
//    void testTransactional() {
//        System.out.println("hello");
//    }

    @PostMapping("/upload")
    void testFileUpload(@RequestParam("file") MultipartFile file) {
        new Thread(() -> {

            try {
                Thread.sleep(1000);
                InputStream in = file.getInputStream();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();
    }

    @GetMapping("/test1")
    public void test1() {
        System.out.println("test1");
    }

    @GetMapping("/test2")
    public void test2() {
        System.out.println("test2");
    }

}
